package com.fbs.wowo.util.watermark;

import com.fbs.wowo.util.common.Constants;
import com.fbs.wowo.util.upload.Oss;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

/**
 * 水印
 */
public class WaterMarkUtils {

    public static String addWaterMark(String smallImgPath,String hash,String worksName,
                                    String type,String author,String bel,String date) {
        try {
            //new一个URL对象  
            URL url=new URL(Constants.CERTIFICATE_TEMPLATE_ADDRESS);
            Image srcImg = ImageIO.read(url);
            //获取图片的宽
            int srcImgWidth = srcImg.getWidth(null);
            //获取图片的高
            int srcImgHeight = srcImg.getHeight(null);
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.setColor(new Color(19, 19, 19,255)); //根据图片的背景设置水印颜色

            g.setFont(new Font("宋体", Font.PLAIN, 48));   //设置字体
            g.drawString(hash,762, 528);  //画出水印

            g.setFont(new Font("宋体", Font.PLAIN, 60));   //设置字体
            //添加二维码
            ImageIcon imgIcon = new ImageIcon(smallImgPath);
            //得到Image对象。
            Image img = imgIcon.getImage();
            //将小图片绘到大图片上,5,300 .表示你的小图片在大图片上的位置。
            g.drawImage(img, 455, 1427, null);

            g.drawString(worksName,762, 670);  //画出水印
            g.drawString(type,762, 876);  //画出水印
            g.drawString(author,762, 1076);  //画出水印
            g.drawString(bel,1924, 1079);  //画出水印
            g.drawString(date,1163, 1583);  //画出水印
            g.dispose();

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bufImg, "jpg",os);
            InputStream is = new ByteArrayInputStream(os.toByteArray());
            MultipartFile multipartFile = new MockMultipartFile("temp.jpg","temp.jpg","", is);
            String imgUrl = Oss.getFileUrl(multipartFile, "微版权证书", Oss.getOssEndpointPrivate(),
                    Oss.getOssAccessKeyId(), Oss.getOssAccessKeySecret(), Oss.getOssBucketName());
            System.out.println("添加水印完成");
            os.flush();
            os.close();
            return imgUrl;
        } catch (Exception e) {
            e.printStackTrace();
            return "生成证书失败!";
        }
    }

/*    public static void main(String[] args){
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        String date=year+"年"+month+"月"+day+"日";

        String url = "http://www.xidata.com.cn";
        String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode";
        String fileName = "temp.jpg";
        String smallImgPath=QrCodeUtil.createQrCode(url,path,fileName);

        String worksName="非洲风情";
        String type="摄影作品";
        String author="魏半全";
        String bel="魏半全";

        String hsah="qwertyuioplkjhgfdsazxcvbnmlkjhgfdsaqwertyuioplllllllllllllllllll";

        String s = addWaterMark(smallImgPath, hsah,worksName, type, author, bel, date);
        System.out.println(s);
    }*/
}